home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: What is &Variable (declared as: char Variable[10])?
- Date: 29 Feb 1996 07:37:53 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb29003753@qcd.lanl.gov>
- References: <4gqpa1$3h9@alcor.usc.edu> <1996Feb26.211807.28858@isac.hces.com>
- <31331a38.54160408@nntp.ix.netcom.com>
- <1996Feb28.195423.10465@isac.hces.com>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: gg@isac.hces.com's message of Wed, 28 Feb 1996 19:54:23 GMT
-
- In article <1996Feb28.195423.10465@isac.hces.com>
- gg@isac.hces.com (Greg Goodrich) writes:
-
- <snip>
- GG: : > scanf("%s", &myarray);
- <snip>
- GG: : The %s format item in scanf expects a pointer to char. myarray is
- GG: : converted to a pointer to char so it is legal. &myarray is a pointer
- GG: : to array of 10 char and is not converted. This results in undefined
- GG: : behavior.
- GG:
- GG: : In many implementations pointer to char and pointer to array of 10
- GG: : char have the same representation and this will work properly, but
- GG: : this is not required by the standard.
- GG:
- GG: I would like to see an example of how this could be implemented to fail.
-
- This is a recurring point of philosophy which takes up a lot of time
- in this group. The point of a language standard is not only to
- standardize existing practice, but to draw up a charter which
- 1) defines which constructs have a meaning
- 2) either defines that meaning or specifies how that meaning is to
- be defined.
- The existence of this charter gives the programmer to be guaranteed a basic
- minimum from an implementation that calls itself `conforming' to this
- standard: so that (s)he can write code without knowing which computer
- it will run on, and is guaranteed that no existing or future design
- `feature', no existing or future implementation on existing or future
- machine with existing or future architecture is going to reject the
- code, or to produce results which are not as defined in this charter;
- till the standard is changed incompatibly. The standards are rarely
- changed incompatibly in major ways, and when they are: one should
- consider it as a new language which supercedes the older one; in
- practice, both languages exist simultaneously for a while ... and to
- support legacy code, old compilers often exist for ever.
-
- On the other hand, once you use features not defined by the standard,
- you are making assumptions about particular compilers, optimization
- techniques, hardware and architecture in writing the code. As it
- violates the defining document of the language, the code you write
- should not even be called as `written in that language'. So, whenever
- someone asks questions like `how could it fail?' in a discussion like
- this, I think that there is a philosophical trouble. Programming
- requires discipline, and rules however arbitrary have to be followed
- if those are the rules that define the language.
-
- Remember, your compiler vendor is trying to implement only the defined
- features ... anything else may work or not work by accident. Remember
- that when a language is extended, it is usually extended in ways which
- do not conflict usages with defined meanings: very often it picks up
- precisely usages which do not have a defined meaning; and define them
- to have a meaning. Are you sure no extension, and no revision of the
- standard, is going to define this construct to have a meaning which
- you did not expect.
-
- Further, you say,
-
- GG: They both point to the exact same memory address, the only difference
- GG: being the datatype of the value, which can be type casted if necessary.
-
- Remember a data type determines not only the range of values, but also
- their representation. Are you sure nobody will have any reason to
- define pointers to arrays to have a different representation than
- pointers to characters? Are you sure nobody will ever write a compiler
- that carries around extra information in array pointers so that it can
- do bounds checking? Are you sure if this information indeed exists, it
- will not mess up the stack when scanf actually accesses what it thinks
- is the character pointer itself?
-
- Do you think there will never be a C implementation where the runtime
- system is smart enough to do type checks to determine whether the
- program makes sense? Are you sure C will never have smart linkers that
- try to check that the right version of the routine is being called
- based upon the number and tupes of the parameters?
-
- If there were a type cast, the compiler would be obliged to change
- the representation if necessary. The type cast wasn't there: that is
- what lead to the undefined behaviour. All this discussion would not
- have started, if there were the cast.
-
- Actually, technically, the meaning of casting a pointer to an array to
- a pointer to its element type is `implementation defined'. Perhaps, by
- mistake, the standard does not specify that it has to point to the
- first (or as some people like to think of it, zeroth) element. I do
- not think anyone would have brought up this point in comp.lang.c
- ... the reason this discussion started is because there was _no
- cast_.
-
- If the cast were there, even if somebody posted a reply, they would
- have pointed out that as &myarray[0] has a defined meaning according
- to the standard, and (char*)&myarray is required to be defined by the
- implementation, it is better to use the first rather than the
- second. I also believe that casts are almost as bad as gotos: there
- are places where they must be used, but they are more commonly used to
- hide either bad design or, worse, errors.
-
- GG: If you have an array, the address of the array is also the address of
- GG: the first member of the array, because the array itself is not a
- GG: pointer, but a reference. The same goes for a structure. The address
- GG: of a struct is the same as the address of the first member of the
- GG: struct, but of different data type. One is pointer to struct and the
- GG: other is pointer to whatever the first member is declared as, but they
- GG: are both pointers, and therefore are the same size.
-
- 1 is a number, so is 1.0 . Do you therefore conclude they have the
- same size??? If you read the FAQ, you will find plenty of examples of
- machines where not all pointers have the same size. Even with the same
- size, they need not have the same representation.
-
- This is basically the mistake which people were trying to point
- out. Nowhere in the standard (or existing practice) do you find the
- requirement that a pointer is just an `address'. A pointer is all the
- information which along with its type suffices to fetch the object: it
- can have any information whatsoever that the implementation feels
- necessary.
-
- And by the way, the standard does mention that a pointer to a union
- _suitably cast_ points to the first member. A similar statement is
- also made for union types. Arrays are the only types left
- out. Nevertheless, for reasons stated above, I would rather use &str.x
- rather than (char*)&str when both are equivalent.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-